package com.xmhmyh.generalframework.project.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.entity.DataGridRequest;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.LogBusType;
import com.xmhmyh.generalframework.common.enums.LogModule;
import com.xmhmyh.generalframework.common.enums.LogSourceType;
import com.xmhmyh.generalframework.framework.aspectj.annotation.Log;
import com.xmhmyh.generalframework.project.sys.dto.RoleDTO;
import com.xmhmyh.generalframework.project.sys.entity.Menu;
import com.xmhmyh.generalframework.project.sys.entity.Role;
import com.xmhmyh.generalframework.project.sys.entity.TreeNode;
import com.xmhmyh.generalframework.project.sys.entity.User;
import com.xmhmyh.generalframework.project.sys.service.IMenuService;
import com.xmhmyh.generalframework.project.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;


/**
 * @Author: 于丁
 * @DateTime: 2020/7/29 6:45 下午
 */
@Api(tags = "角色相关接口")
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Log(busType = LogBusType.EDITANDSAVE, source = LogSourceType.MANAGE, module = LogModule.ROLEMANAGE)
    @PreAuthorize("hasAuthority('system:role:save')")
    @ApiOperation(value = "保存角色接口")
    @PostMapping("save")
    public Response saveHandler(@RequestBody RoleDTO roleDTO) {
        roleService.saveHandler(getLoginUser().getUser(), roleDTO);
        return Response.success();
    }


    @Log(busType = LogBusType.DELETE, source = LogSourceType.MANAGE, module = LogModule.ROLEMANAGE)
    @PreAuthorize("hasAuthority('system:role:delete')")
    @ApiOperation(value = "删除角色")
    @PostMapping("del")
    public Response delHandler(Long id) {
        roleService.delHandler(id);
        return Response.success();
    }

    @PreAuthorize("hasAuthority('system:role:list')")
    @ApiOperation(value = "查找角色列表")
    @PostMapping("pagination")
    public Response paginationHandler(@RequestBody DataGridRequest params) {
        params = addAccountConstraints(params);
        IPage<Role> roleIPage = roleService.paginate(params);
        return Response.success(roleIPage);
    }

    @PreAuthorize("hasAnyAuthority('system:role:edit','system:role:add')")
    @ApiOperation(value = "查找角色权限树")
    @PostMapping("getMenuTree")
    public Response getMenuTreeHandler() {
        User user = getLoginUser().getUser();
        Collection<Menu> menus = menuService.getMenuByUserId(user.getId());
        Collection<TreeNode> treeNodes = menuService.buildMenuTree(menus);
        return Response.success(treeNodes);
    }

    @PreAuthorize("hasAnyAuthority('system:role:edit','system:role:add')")
    @ApiOperation(value = "获取用户已经有的权限")
    @PostMapping("loadByRoleId")
    public Response loadByRoleIdHandler(@RequestParam Long roleId) {
        List<Long> treeList = roleService.loadByRoleIdHandler(roleId);
        return Response.success(treeList);
    }


    @PreAuthorize("hasAuthority('system:role:edit')")
    @ApiOperation(value = "获取角色的详细数据")
    @PostMapping("loadMore")
    public Response loadMoreHandler(@RequestParam Long id) {
        Role role = roleService.getById(id);
        return Response.success(role);
    }
}
